package com.whr.controller;

import com.whr.bean.Columns;
import com.whr.bean.Resources;
import com.whr.service.ResService;
import com.whr.utils.CompareResources;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/res")
public class ResController {
    @Resource
    private ResService resService;

    @RequestMapping("/list/{page}/{old}")
    public String list(@PathVariable("page") int page, @PathVariable("old") int old, Model model) {
        List<Resources> reses0 = resService.findAll();
        List<Resources> reses1 = new CompareResources().sortRes(reses0);
        int size = 16;//每页显示条目个数
        int total = reses1.size();
        List<Resources> reses = new ArrayList<>();
        int p = (page - 1) * size;
        for (int i = 0; i < size; i++) {
            if ((p + i) < total) {
                reses.add(reses1.get(p + i));
            }
        }
        model.addAttribute("reses", reses);
        model.addAttribute("total", total);
        model.addAttribute("size", size);
        model.addAttribute("page", page);
        model.addAttribute("old", old);
        return "resList";
    }

    @RequestMapping("/add")
    public String add(Resources res) {
        resService.saveRes(res);
        return "redirect:/res/list/1/1";
    }

    @RequestMapping("/stop")
    public String stop(Integer[] checks) {
        resService.stop(checks);
        return "redirect:/res/list/1/1";
    }

    @RequestMapping("/start")
    public String start(Integer[] checks) {
        resService.start(checks);
        return "redirect:/res/list/1/1";
    }

    @RequestMapping("/toadd")
    public String toadd(Model model) {
        List<Resources> reses0 = resService.findAll();
        List<String> menus = new CompareResources().sortMenu(reses0);
        model.addAttribute("menus", menus);
        return "resAdd";
    }

    @RequestMapping("/toedit")
    public String toedit(Integer checks, Model model) {
        Resources res = resService.findById(checks);
        model.addAttribute("res", res);
        List<Resources> reses0 = resService.findAll();
        List<String> menus = new CompareResources().sortMenu(reses0);
        model.addAttribute("menus", menus);
        return "resEdit";
    }

    @RequestMapping("/edit")
    public String edit(Resources res) {
        resService.updateRes(res);
        return "redirect:/res/list/1/1";
    }

    @RequestMapping("/del")
    public String del(Integer[] checks) {
        resService.delRes(checks);
        return "redirect:/res/list/1/1";
    }

    @RequestMapping("/column")
    public String colunm(Model model) {
        List<Columns> columns = resService.findCol();
        model.addAttribute("columns", columns);
        return "column";
    }

    @RequestMapping("/vbl")
    public String pass(Integer[] checks) {
        resService.vbl(checks);
        return "redirect:/res/column";
    }

    @RequestMapping("/unvbl")
    public String unvbl(Integer[] checks) {
        resService.unvbl(checks);
        return "redirect:/res/column";
    }

    @RequestMapping("/upcolumn/{cid}")
    public String upcolumn(@PathVariable("cid") Integer cid, Model model) {
        Columns column = resService.findColById(cid);
        model.addAttribute("column", column);
        return "editcolumn";
    }

    @RequestMapping("/editcol")
    public String editcol(Columns column) {
        resService.updateCol(column);
        return "redirect:/res/column";
    }
}
